<?php
/*
 *  This file is part of Urd.
 *
 *  Urd is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *  Urd is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. See the file "COPYING". If it does not
 *  exist, see <http://www.gnu.org/licenses/>.
 *
 * $LastChangedDate: 2008-07-15 15:42:06 +0200 (Tue, 15 Jul 2008) $
 * $Rev: 1296 $
 * $Author: gavinspearhead $
 * $Id: browse.php 1296 2008-07-15 13:42:06Z gavinspearhead $
 */
if (!defined('ORIGINAL_PAGE')) define('ORIGINAL_PAGE', $_SERVER['PHP_SELF']);

$pathpb = realpath(dirname(__FILE__));


require_once "$pathpb/../functions/html_includes.php";
//require_once "$pathpb/../functions/smarty.php";

//var_dump($smarty);

if (!isset($_SESSION['setdata']) || !is_array($_SESSION['setdata'])) 
	$_SESSION['setdata'] = array();

// TODO Challenge

if (!isset($_REQUEST['command'])) die('No command given');

$command = $_REQUEST['command'];
if (isset($_REQUEST['type']))
	$type = $_REQUEST['type'];
else $type = 'none';

switch ($command) {
case 'add':	
	$setID = GetSetID();
	AddToBasket($setID, $type);
	break;
case 'del':	
	$setID = GetSetID();
	DelFromBasket($setID, $type);
	break;
case 'clear':	
	ClearBasket();
	break;
case 'view':	
	DisplayBasket($db);
	break;
default:	
	die(); // no command, do nothing
}


die();
// We don't get beyond this, only functions:


function AddToBasket($setID, $type)
{
	if (!isset($_SESSION['setdata']))
	       $_SESSION['setdata'] = array();
	if (!in_setdata($setID, $type, $_SESSION['setdata'])) {
		$set = array('setid'=> $setID, 'type'=>$type);
		$_SESSION['setdata'][] = $set;
	}
}

function DelFromBasket($setID, $type)
{
	$newarray = array();
	foreach ($_SESSION['setdata'] as $set) {
		if ($set['setid'] != $setID) 
			$newarray[] = $set;
	}
	$_SESSION['setdata'] = $newarray;
}

function ClearBasket()
{
	$_SESSION['setdata'] = array();
}

function GetSetID()
{
	if (!isset($_REQUEST['setID'])) 
		die('no setID given');
	return $_REQUEST['setID'];
}

function DisplayBasket(DatabaseConnection $db)
{
	global $smarty, $LN;
	$addedsets = array();
	$totalsize = $cnt = 0;
	$bettersetname = '';

	$dlsetname = isset($_POST['dlsetname'])? $_POST['dlsetname']:'';

	// For each set that's in the basket:
	foreach ($_SESSION['setdata'] as $set) {
		$setID = $set['setid'];
		$type = $set['type'];
		if ($type == 'group') {
			$res = $db->select_query("setdata.\"subject\", setdata.\"size\", extsetdata.\"value\" FROM (setdata LEFT JOIN extsetdata ON "
			       . " setdata.\"ID\" = extsetdata.\"setID\" AND extsetdata.\"name\" = 'setname' AND extsetdata.\"type\" = '" . USERSETTYPE_GROUP . "') where \"ID\" = '$setID'");
			if ($res === FALSE)
				continue;
			$setname = utf8_decode($res[0]['subject']);
			$setsize = $res[0]['size'];
			$extsetname = $res[0]['value'];
		} else if ($type == 'rss') {
			$res =  $db->select_query("\"setname\", \"size\", extsetdata.\"value\"  FROM (rss_sets LEFT JOIN extsetdata ON " . 
					"rss_sets.\"setid\" = extsetdata.\"setID\" AND extsetdata.\"name\" = 'setname' AND extsetdata.\"type\" = '" . USERSETTYPE_RSS . "') WHERE rss_sets.\"setid\" = '$setID'");
			if ($res === FALSE)
				continue;
			$setname = utf8_decode($res[0]['setname']);
			$extsetname = $res[0]['value'];
			$setsize = $res[0]['size'];
		} else 
			throw new exception ('Unknown type: ' .$type);
		$totalsize += $setsize;
		$setsize = readable_size($setsize, 1 , $LN['byte_short']);
		// If there's extsetinfo, create a fancy download name:
		if ($extsetname != '') 
		 	$bettersetname = create_extset_download_name($db, $setID);
		else {
			// No extsetinfo, use the original setname:
			$bettersetname = $setname;
		}

		$addedsets[] = array('subject'=>$bettersetname, 'size'=>$setsize);
		$cnt ++;
	}

	// If this is the 1st item in the basket, use its bettersetname as the download name:
	if ($cnt > 0 && $dlsetname == '') {
		$dlsetname = find_name($db, $bettersetname);
		$_SESSION['dlsetname'] = $dlsetname;
	}

	$totalsize = readable_size($totalsize, 0, $LN['byte_short']);

	$smarty->assign('dlsetname', $dlsetname);
	$smarty->assign('addedsets', $addedsets);
	$smarty->assign('totalsize', $totalsize);

	$smarty->display('ajax_showbasket.tpl');
}
?>
